--------------------------
-- edu 28/08/2020
--------------------------

CREATE OR REPLACE VIEW public.cubo_art_rubros AS
SELECT x.idart,
       x.r1, artr1.nombre as r1_nombre,
       x.r2, artr2.nombre as r2_nombre,
       x.r3, artr3.nombre as r3_nombre,
       x.r4, artr4.nombre as r4_nombre,
       x.r5, artr5.nombre as r5_nombre,
       x.r6, artr6.nombre as r6_nombre,
       x.r7, artr7.nombre as r7_nombre,
       x.r8, artr8.nombre as r8_nombre,
       x.r9, artr9.nombre as r9_nombre,
       x.r10, artr10.nombre as r10_nombre
FROM
(
 SELECT art.id AS idart,
    str_al_punto(art.codartr, 1) AS r1,
    str_al_punto(art.codartr, 2) AS r2,
    str_al_punto(art.codartr, 3) AS r3,
    str_al_punto(art.codartr, 4) AS r4,
    str_al_punto(art.codartr, 5) AS r5,
    str_al_punto(art.codartr, 6) AS r6, 
    str_al_punto(art.codartr, 7) AS r7, 
    str_al_punto(art.codartr, 8) AS r8, 
    str_al_punto(art.codartr, 9) AS r9, 
    str_al_punto(art.codartr, 10) AS r10
   FROM art
) x
LEFT JOIN artr artr1 ON artr1.tipoarta = 'A' and artr1.cod = r1
LEFT JOIN artr artr2 ON artr2.tipoarta = 'A' and artr2.cod = r2
LEFT JOIN artr artr3 ON artr3.tipoarta = 'A' and artr3.cod = r3
LEFT JOIN artr artr4 ON artr4.tipoarta = 'A' and artr4.cod = r4
LEFT JOIN artr artr5 ON artr5.tipoarta = 'A' and artr5.cod = r5
LEFT JOIN artr artr6 ON artr6.tipoarta = 'A' and artr6.cod = r6
LEFT JOIN artr artr7 ON artr7.tipoarta = 'A' and artr7.cod = r7
LEFT JOIN artr artr8 ON artr8.tipoarta = 'A' and artr8.cod = r8
LEFT JOIN artr artr9 ON artr9.tipoarta = 'A' and artr9.cod = r9
LEFT JOIN artr artr10 ON artr10.tipoarta = 'A' and artr10.cod = r10
ORDER BY 1;

ALTER TABLE public.cubo_art_rubros OWNER TO abacouser;


-- =================================
SELECT aaversion('439.01', '439.00');
-- =================================

